package com.iterator.controller.wechat.dhy;


import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.bean.wechat.dhy.LoginBo;
import com.iterator.bean.wechat.dhy.ResultVO;
import com.iterator.bean.wechat.dhy.WxRegisterBO;
import com.iterator.bean.wechat.dhy.WxUserLoginVO;
import com.iterator.config.AliyunComponent;
import com.iterator.realm.CustomToken;

import com.iterator.service.web.systemManager.SystemManageService;
import com.iterator.util.IteratorEncrypt;
import com.iterator.util.UserNameAndPasswordValidatorUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {
    @Autowired
    SystemManageService systemManageService;
    @Autowired
    AliyunComponent  aliyunComponent;


    /**
     * wx 登录
     * @param loginBo
     * @return
     */
    @PostMapping("login")
    public ResultVO login(@RequestBody LoginBo loginBo){
        String username = loginBo.getUsername();
        //判断传入的是用户名还是手机号
        String trueName= systemManageService.judgeUserName(username);
        //密码加密用于比对
        String encryptPassword = IteratorEncrypt.getEncryptPassword(loginBo.getPassword());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new CustomToken(trueName,encryptPassword,"vx"));
        }catch (AuthenticationException e){
            return ResultVO.error(402, "参数值不对");
        }
        WxUserLoginVO loginVo = systemManageService.login(trueName, subject.getSession().getHost());
        if (loginVo == null)
            return ResultVO.error(402, "参数值不对");
        String id = (String) subject.getSession().getId();
        loginVo.setToken(id);

        return ResultVO.ok(loginVo);
    }

    /**
     * wx 登出
     * @return
     */
    @PostMapping("logout")
    public ResultVO logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResultVO.ok();
    }


    /**
     * wx注册时的验证码
     * @param
     * @return
     */
    @PostMapping("regCaptcha")
    public BaseRespVo getCode(@RequestBody Map map , HttpServletRequest request) {
        String mobile = (String) map.get("mobile");
        //随机生成6位验证码
        Random random = new Random();
        String code ="";
        for (int i = 0; i < 6; i++) {
            int i1 = random.nextInt(9);
            code += i1;
        }
        request.getServletContext().setAttribute("code",code);
        aliyunComponent.sendMsg(mobile,code);
        return BaseRespVo.ok("验证码发送成功");
    }

    /**
     * 用户注册
     */
    @PostMapping("register")
    public BaseRespVo register(@RequestBody WxRegisterBO wxRegisterBO,HttpServletRequest request) {
        String code = (String) request.getServletContext().getAttribute("code");

        int status= systemManageService.register(wxRegisterBO,code);
        if (status == 502) {
            return BaseRespVo.fail("验证码错误");
        } else if (status == 505){
            return BaseRespVo.fail("用户名重复");
        } else if (status == 508) {
            return BaseRespVo.fail("手机号码已经被注册");
        }
        String username = wxRegisterBO.getUsername();
        String password = wxRegisterBO.getPassword();
        //正则校验
        String s = UserNameAndPasswordValidatorUtil.userNameAndPasswordValidateUtil(username, password);
        if (s != null) {
            //正则校验不通过
            return BaseRespVo.fail(s);
        }
        //加密过的密码
        String encryptPassword = IteratorEncrypt.getEncryptPassword(password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(new CustomToken(username,encryptPassword,"vx"));
        String host = subject.getSession().getHost();
        WxUserLoginVO login = systemManageService.login(username, host);
        String id = (String) subject.getSession().getId();
        login.setToken(id);
        return BaseRespVo.ok(login);
    }

    /**
     * wx 用户密码重置
     * @return
     */
    @PostMapping("reset")
    public BaseRespVo resetPassword(@RequestBody Map map,HttpServletRequest request) {
        String newCode = (String) request.getServletContext().getAttribute("code");
        String code = (String) map.get("code");
        String mobile = (String) map.get("mobile");
        String password = (String) map.get("password");
        //加密密码
        String encryptPassword = IteratorEncrypt.getEncryptPassword(password);
        int status=systemManageService.resetPassword(code,mobile,encryptPassword,newCode);
        if (status == 502) {
            return BaseRespVo.fail("验证码不正确");
        } else if (status == 503) {
            return BaseRespVo.fail("手机号不存在");
        }
        return BaseRespVo.ok();
    }
}
